package com.service.imp;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dao.DeptDao;
import com.dao.StaffDao;
import com.model.DeptBean;
import com.model.Messages;
import com.model.StaffBean;

@Service
@Transactional(rollbackFor = { RuntimeException.class }, propagation = Propagation.REQUIRED)
public class StaffService {
	@Autowired
	private DeptDao deptDao;
	@Autowired
	private StaffDao staffDao;
	@Autowired
	private JdbcTemplate jdbcTemplate;
	public Messages editStaff(StaffBean staff,String dept_id) {
		Messages m= new Messages();
		System.out.println(staff.getId());
		StaffBean staffBean = this.staffDao.get(staff.getId());
		
		staffBean.setStaff_address(staff.getStaff_address());
		staffBean.setStaff_date_birth(staff.getStaff_date_birth());
		staffBean.setStaff_eb(staff.getStaff_eb());
		staffBean.setStaff_hire(staff.getStaff_hire());
		staffBean.setStaff_id_card(staff.getStaff_id_card());
		staffBean.setStaff_name(staff.getStaff_name());
		staffBean.setStaff_number(staff.getStaff_number());
		staffBean.setStaff_phone(staff.getStaff_phone());
		staffBean.setStaff_post(staff.getStaff_post());
		staffBean.setStaff_salary(staff.getStaff_salary());
		
		staffBean.setStaff_sex(staff.getStaff_sex());
		DeptBean db= this.deptDao.get(Integer.parseInt(dept_id));
		
		staffBean.setDept_id(db);
		m.setInfo(true);
		m.setText("修改成功");
		return m;
	}
	
	public Messages addStaff(StaffBean staff,String dept_id) {
		Messages m= new Messages();
		DeptBean db=this.deptDao.get(Integer.parseInt(dept_id));
		if(db!=null)
			staff.setDept_id(db);
		else throw new RuntimeException("参数为空");
		staff.setIs_turnover(false);
		this.staffDao.save(staff);
		m.setInfo(true);
		m.setText("添加成功");
		return m;
	}
	/**
	 * 获取部门下员工
	 * @param dept_id
	 * @param sortField 
	 * @param orderType 
	 * @param maxrecord 
	 * @param currentPage 
	 * @return
	 */
	public List<Map<String,Object>> getstaffBydept(String dept_id, int currentPage, int maxrecord, String orderType, String sortField) {
		List<Map<String,Object>> alllist = new ArrayList<Map<String,Object>>();
		StringBuffer sb2= new StringBuffer();
		sb2.append("select a.id,");
		sb2.append("a.staff_number,");
		sb2.append("a.staff_name,a.staff_sex,a.staff_eb,");
		sb2.append("a.staff_phone,a.staff_address,NVL(a.staff_post,'暂无') as staff_post,");
		sb2.append("a.staff_id_card,a.staff_hire,a.staff_date_birth,");
		sb2.append("a.staff_salary");
		sb2.append(" from tb_staff a where a.is_turnover=0 and a.dept_id='");
		sb2.append(dept_id);
		sb2.append("' order by a.staff_number asc");
		alllist=this.jdbcTemplate.queryForList(sb2.toString());
		return alllist;
	}
	/**
	 * 删除员工
	 * @param id
	 * @return
	 */
	public Messages delStaff(String id) {
		Messages m= new Messages();
		System.out.println("删除id"+id);
		if(id!=null&&!"".equals(id)){
			String sql ="delete from tb_staff t where t.id='"+id+"'";
			this.jdbcTemplate.update(sql);
			m.setInfo(true);
			m.setText("删除成功");
		}else{
			m.setInfo(false);
			m.setText("删除员工出错");
		}
		return m;
	}

	public List<Map<String, Object>> getstaffBydeptToname(String dept_id) {
		List<Map<String,Object>> alllist = new ArrayList<Map<String,Object>>();
		StringBuffer sb2= new StringBuffer();
		sb2.append("select ");
		sb2.append("a.staff_name");
		sb2.append(" from tb_staff a where a.is_turnover=0 and a.dept_id='");
		sb2.append(dept_id);
		sb2.append("' and rownum<15 order by a.staff_number asc");
		alllist=this.jdbcTemplate.queryForList(sb2.toString());
		return alllist;
	}

}
